from piston.handler import BaseHandler
import datetime
from django.utils import simplejson

from models import Users
from models import SocialAccount
from models import Message

class CalcHandler( BaseHandler ):
		allow_method =('GET', 'POST','PUT', 'DELETE')
		
		#for GET method
		@classmethod
		def read( self, request, expression ):
				#CalcHandler.saveNewUser()
				emailaddr=request.GET.get('email')
				pwds=request.GET.get('password')
				record = Users.objects.get(email_address=emailaddr, password=pwds)
				return CalcHandler.produceJSON(record)
		
		#for POST method
		@classmethod
		def create( self, request, expression):
				#parse json?
				json_data = simplejson.loads(request.raw_post_data)
				new_user = Users(
				name = json_data['name'],
				email_address = json_data['email_address'], #expression['email_address'],				
				password = json_data['password'] #expression['password']
				)
				new_user.save()
				return
		
		#for PUT method, define update method
		#for DELETE method, define delete method
					
		@staticmethod
		def produceJSON(retobj):
			retval = {}
			retval['name'] = retobj['name']
			retval['email_address'] = retobj.email_address
			return retval
			
		@staticmethod
		def saveNewUser():
			new_user=Users(
				name="yang",
				password="123",
				email_address="yang@cmu.edu",
				university="CMU", 
				credit_score=0.0, 
				friend_list=[0],
				group_list=[0],
				socialnetwork_account=[
				SocialAccount
				(
					network_type="facebook",
					username="yang",
					password="123"
				)
				],
				messagebox=[
				Message
				(
					sender_email="kai@cmu.edu",
					receiver_email="yang@cmu.edu",
					send_date=datetime.datetime.strptime("2012-04-12 15:30","%Y-%m-%d %H:%M"),
					content="hello!",
					carpool_no=0, 
					msg_type="regular", 
					msg_status="done"
				)
				]
			)
			new_user.save()
			return